package com.hxd.otc.dialog;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.dialog.widget.base.BottomBaseDialog;
import com.hxd.otc.R;
import com.hxd.otc.api.Constant;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.module.my.model.UserModel;
import com.hxd.otc.mvp.base.IBaseView;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.ResourceUtil;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.BlocksToast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lzy on 2018/6/14 0014.
 * description ${TODO}
 * <p>
 * Changed by somebody on 2018/6/14 0014
 * description ${TODO}
 */
public class EditAccountInfoDialog extends BottomBaseDialog<EditAccountInfoDialog> {

    private final IBaseView mIBaseView;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_label)
    TextView mTvLabel;
    @BindView(R.id.et_input)
    EditText mEtInput;
    @BindView(R.id.iv_clear)
    ImageView mIvClear;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;
    private OnConfirmClickListener onConfirmClickListener;
    public static final String MODIFY_TYPE_NAME = "name";
    public static final String MODIFY_TYPE_NICKNAME = "nickName";
    public static final String MODIFY_TYPE_CHECKPWD = "checkPwd";

    private String mModifyType = MODIFY_TYPE_NAME;
    private String mDefaultText;

    public EditAccountInfoDialog(Context context) {
        super(context);
        mIBaseView = (IBaseView) context;
    }

    @Override
    public View onCreateView() {
        return View.inflate(mContext, R.layout.dialog_account_info_edit, null);
    }

    @Override
    public void onViewCreated(View view) {
        ButterKnife.bind(this, view);
        mEtInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mIvClear.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void setUiBeforShow() {
        updateUI();
    }

    private void updateUI() {
        if (MODIFY_TYPE_NAME.equals(mModifyType)) {
            showModifyNameUI();
        } else if (MODIFY_TYPE_NICKNAME.equals(mModifyType)) {
            showModifyNickNameUI();
        } else {
            showCheckPasswordUI();
        }
    }


    /**
     * 验证登录密码界面
     */
    private void showCheckPasswordUI() {
        mTvTitle.setVisibility(View.VISIBLE);
        mTvTitle.setText(R.string.my_input_login_password);
        mTvLabel.setText(R.string.login_password);
        UIUtils.setLoginPasswordEditText(mEtInput);
        mEtInput.setHint(ResourceUtil.getString(R.string.my_modify_login_password_require, Constant.LOGIN_PASSWORD_MIN_LENGTH, Constant.LOGIN_PASSWORD_MAX_LENGTH));
        mTvConfirm.setText(R.string.my_affirm);
        mTvCancel.setVisibility(View.GONE);
    }

    /**
     * 修改名称界面
     */
    private void showModifyNameUI() {
        mTvTitle.setVisibility(View.GONE);
        mTvLabel.setText(R.string.my_name);
        mEtInput.setHint(R.string.please_input_new_name);
        mEtInput.setInputType(InputType.TYPE_CLASS_TEXT);
        mEtInput.setFilters(new InputFilter[]{});
        mEtInput.setFilters(new android.text.InputFilter[]{
                new android.text.InputFilter.LengthFilter(Constant.MAX_NAME_LENGTH)});
        mTvCancel.setVisibility(View.VISIBLE);
        mTvConfirm.setText(R.string.my_save);
        mEtInput.setText(mDefaultText);
        mEtInput.setSelection(mDefaultText.length());
    }

    /**
     * 修改昵称界面
     */
    private void showModifyNickNameUI() {
        mTvTitle.setVisibility(View.GONE);
        mTvLabel.setText(R.string.my_nickname);
        mEtInput.setHint(R.string.please_input_new_nickname);
        mEtInput.setFilters(new InputFilter[]{});
        mEtInput.setFilters(new android.text.InputFilter[]{
                new android.text.InputFilter.LengthFilter(Constant.MAX_NICKNAME_LENGTH)});
        mTvCancel.setVisibility(View.VISIBLE);
        mTvConfirm.setText(R.string.my_save);
        mEtInput.setText(mDefaultText);
        mEtInput.setSelection(mDefaultText.length());
    }


    @OnClick({R.id.tv_confirm, R.id.tv_cancel, R.id.iv_clear})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_confirm:
                onConfirm();
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.iv_clear:
                mEtInput.setText("");
                break;
        }
    }

    private void onConfirm() {
        if (MODIFY_TYPE_NAME.equals(mModifyType)) {
            onModifyName();
        } else if (MODIFY_TYPE_NICKNAME.equals(mModifyType)) {
            onModifyNickName();
        } else {
//            onCheckPwd();
        }
    }

    /**
     * 修改名称
     */
    private void onModifyName() {
        final String name = mEtInput.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            BlocksToast.get().showToast(mEtInput.getHint().toString());
            return;
        }
        if (name.length() < Constant.MIN_NAME_LENGTH) {
            BlocksToast.get().showToast(String.format(ResourceUtil.getString(R.string.my_name_min_lenght_require), Constant.MIN_NAME_LENGTH));
            return;
        }
        UserModel.getInstance().updatename(name, new BaseObserver<Object>() {
            @Override
            public void onNext(String msg, Object o) {
                BlocksToast.get().showToast(msg);
                UserHelper.putName(name);
                if (onConfirmClickListener != null) {
                    onConfirmClickListener.onModifyNameSuccess(name);
                }
                dismiss();

            }

            @Override
            public void onError(int errType, String errMessage) {
                BlocksToast.get().showToast(errMessage);
            }

        }, mIBaseView.<BaseBean>bindAutoDispose());
    }

    /**
     * 修改昵称
     */
    private void onModifyNickName() {
        final String nickName = mEtInput.getText().toString().trim();
        if (TextUtils.isEmpty(nickName)) {
            BlocksToast.get().showToast(mEtInput.getHint().toString());
            return;
        }
        if (nickName.length() < Constant.MIN_NICKNAME_LENGTH) {
            BlocksToast.get().showToast(String.format(ResourceUtil.getString(R.string.my_nickname_min_lenght_require), Constant.MIN_NICKNAME_LENGTH));
            return;
        }
        UserModel.getInstance().updatenick(nickName, new BaseObserver<String>() {
            @Override
            public void onNext(String msg, String o) {
                BlocksToast.get().showToast(msg);
                UserHelper.updateNickName(nickName);
                if (onConfirmClickListener != null) {
                    onConfirmClickListener.onModifyNickNameSuccess(nickName);
                }
                dismiss();
            }

            @Override
            public void onError(int errType, String errMessage) {
                BlocksToast.get().showToast(errMessage);
            }

        }, mIBaseView.<BaseBean<String>>bindAutoDispose());
    }

    /**
     * @param type One of  {@link #MODIFY_TYPE_CHECKPWD},{@link #MODIFY_TYPE_NAME} or{@link #MODIFY_TYPE_NICKNAME}
     */
    public EditAccountInfoDialog setModifyType(String type) {
        mModifyType = type;
        return this;
    }

    public EditAccountInfoDialog setOnConfirmClickListener(EditAccountInfoDialog.OnConfirmClickListener onConfirmClickListener) {
        this.onConfirmClickListener = onConfirmClickListener;
        return this;
    }

    public EditAccountInfoDialog setDefaultText(String defaultText) {
        mDefaultText = TextUtils.isEmpty(defaultText) ? "" : defaultText;
        return this;
    }

    public interface OnConfirmClickListener {
        /**
         * 仅在校验登录密码后回调
         */
        void onCheckPasswordSuccess();

        /**
         * 仅在修改名字成功后回调
         *
         * @param newName 新的名字
         */
        void onModifyNameSuccess(String newName);

        /**
         * 仅在修改昵称成功后回调
         *
         * @param newNickName
         */
        void onModifyNickNameSuccess(String newNickName);
    }
}
